package com.agsmobi.digitapp;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

public class ScoreCounterActivity extends BaseActivity implements OnClickListener{
	
	protected int duration;
	protected int completeSound;
	protected int countingUpSound;
	protected int fromScore;
	protected int toScore;
	protected int positionX,positionY,width,length;
	
	private Button btnStart;
	//private EditText editResult;
	private EditText editFrom;
	private EditText editTo; 
	private LinearLayout liResult;
	private ArrayList<ImageView> arrImages;
	private boolean flag = true;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scorecounter);
		loadControl();
		loadData();
	}

	@Override
	protected void loadControl() {
		// TODO Auto-generated method stub
		super.loadControl();
		btnStart = (Button)findViewById(R.id.btnStart);
		btnStart.setOnClickListener(this);
		//editResult = (EditText)findViewById(R.id.editResult);
		liResult = (LinearLayout)findViewById(R.id.liResult);
		editFrom = (EditText)findViewById(R.id.editFrom);
		editTo = (EditText)findViewById(R.id.editTo);
	}

	@Override
	protected void loadData() {
		// TODO Auto-generated method stub
		super.loadData();
		duration = 200;
		positionX = 200;
		positionY = 200;
		width = 350;
		length = 100;
		LayoutParams params = new LayoutParams(width, length);
		params.setMargins(positionX, positionY, 0,0);
		liResult.setLayoutParams(params);
	}
	
	/*
	 * start
	 */
	 private void start(){
		flag = false;
		startTask(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					for(int i = fromScore;i<=toScore;i++){
						handlerDisplay.sendEmptyMessage(i);
						Thread.sleep(duration);
					}
				} catch (Exception ex) {
					Log.d("Error", ex.getMessage());
				}
			}
		});
	}
	/*
	 * 
	 */
	Handler handlerDisplay = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			int index = msg.what;
			String strIndex = String.valueOf(index);
			for(int i = 0;i<strIndex.length();i++){
				index = Integer.parseInt(String.valueOf(strIndex.charAt(i)));
				arrImages.get(i).setBackgroundResource(imageScore[index]);
			}
			if(strIndex.equals(String.valueOf(toScore))){
				flag = true;
			}
			//editResult.setText(String.valueOf(index));
		}
	};
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		if(id == R.id.btnStart){	
			if(editFrom.getText().toString() == null || editTo.getText().toString() == null){
				Toast.makeText(this,getString(R.string.title_required),Toast.LENGTH_LONG).show();
			}else{
				if(flag == true){
					fromScore = Integer.parseInt(editFrom.getText().toString());
					
					String strToScore = editTo.getText().toString();
					toScore = Integer.parseInt(strToScore);
					
					arrImages = new ArrayList<ImageView>();
					liResult.removeAllViews();
					for(int i = 0;i<strToScore.length();i++){
						ImageView temp = new ImageView(this);
						arrImages.add(temp);
						liResult.addView(temp);
					}
					start();
				}
			}
		}
	}
	
}








